package com.eliza.base.views.component.progress

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

//    progress：指示器显示的当前进度。在 0.0 和 1.0 之间传递 Float。
//    color：实际指示器的颜色。也就是说，反映进度的组件部分会在进度完成时完全包围组件。
//    trackColor：绘制指示器的轨道的颜色。

object ProgressX {
    @Composable
    fun Show(navController: NavController) {
        var statex by remember {
            mutableIntStateOf(0)
        }
        Column {
            LinearDeterminateIndicator()
            IndeterminateCircularIndicator()
            IndeterminateLinearProgressIndicator()
        }
    }

    @Composable
    private fun LinearDeterminateIndicator() {

        var currentProgress by remember { mutableStateOf(0f) }
        var loading by remember { mutableStateOf(false) }
        val scope = rememberCoroutineScope() // Create a coroutine scope

        Column(
            verticalArrangement = Arrangement.spacedBy(12.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxWidth()
        ) {
            Text(
                text = "确定指示符\n" +
                        "确定性指示符可准确反映操作完成的程度"
            )
            Button(onClick = {
                loading = true
                scope.launch {
                    loadProgress { progress ->
                        currentProgress = progress
                    }
                    loading = false // Reset loading when the coroutine finishes
                }
            }, enabled = !loading) {
                Text("Start loading")
            }

            if (loading) {
                LinearProgressIndicator(
                    progress = { currentProgress },
                    modifier = Modifier.fillMaxWidth(),
                )
            }
        }
    }

    /** Iterate the progress value */
    suspend fun loadProgress(updateProgress: (Float) -> Unit) {
        for (i in 1..100) {
            updateProgress(i.toFloat() / 100)
            delay(100)
        }
    }

    @Composable
    private fun IndeterminateCircularIndicator() {
        var loading by remember { mutableStateOf(false) }
        Button(onClick = { loading = true }, enabled = !loading) {
            Text("Start loading")
        }

        if (!loading) return

        CircularProgressIndicator(
            modifier = Modifier.width(64.dp),
            color = MaterialTheme.colorScheme.secondary,
            trackColor = MaterialTheme.colorScheme.surfaceVariant,
        )
    }

    @Composable
    private fun IndeterminateLinearProgressIndicator() {
        var loading by remember { mutableStateOf(false) }
        Button(onClick = { loading = true }, enabled = !loading) {
            Text("Start loading")
        }

        if (!loading) return

        LinearProgressIndicator(
            modifier = Modifier.width(64.dp),
            color = MaterialTheme.colorScheme.secondary,
            trackColor = MaterialTheme.colorScheme.surfaceVariant,
        )
    }


}
